package com.deao.ystar.ypositiondemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

import com.deao.ystar.ysgesslib.opsenudorange.ResultLister;
import com.ystar.open.LocationPoint;
import com.ystar.open.YPosition;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity  implements ResultLister {
    TextView tvx ,tvy ,tvz ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvx=findViewById(R.id.tv_x);
        tvy=findViewById(R.id.tv_y);
        tvz=findViewById(R.id.tv_z);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            startActivity(getAppDetailSettingIntent());
            System.out.println("请设置权限先！！！");
            return;
        }
        //6颗卫星的填写数据，准备好并需要传入
        LocationPoint locationPoint1 = new LocationPoint(1070000, 92100, 34, 0);
        LocationPoint locationPoint2 = new LocationPoint(1070000, 16500, 34, 0);
        LocationPoint locationPoint3 = new LocationPoint(71700, 36591, 34, 0);
        LocationPoint locationPoint4 = new LocationPoint(71700, 78502, 34, 5);
        LocationPoint locationPoint5 = new LocationPoint(142600, 37111, 34, 0);
        LocationPoint locationPoint6 = new LocationPoint(142600, 77988, 34, 0);
        List<LocationPoint> satellites = new ArrayList<>();
        satellites.add(locationPoint1);
        satellites.add(locationPoint2);
        satellites.add(locationPoint3);
        satellites.add(locationPoint4);
        satellites.add(locationPoint5);
        satellites.add(locationPoint6);
        YPosition.init( MainActivity.this, satellites.toArray(new LocationPoint[satellites.size()]));

    }

    @Override
    protected void onResume() {
        super.onResume();
        YPosition.reGisterResultLister(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        YPosition.unRegisterResultLister();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        YPosition.unRegisterAll();
    }

    @Override
    public void string2log(String s) {
    }

    //定位回调 LocationPoint 含 x y z 坐标
    @Override
    public void locationReciver(final LocationPoint locationPoint) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    tvx.setText(locationPoint.x+"");
                    tvy.setText(locationPoint.y+"");
                    tvz.setText(locationPoint.z+"");
            }
        });
    }



    //跳转设置权限
    private Intent getAppDetailSettingIntent() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        }
        return localIntent;
    }


}
